package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.system.entity.BizEquipmentEntity;
import org.jeecg.modules.system.mapper.BizEquipmentMapper;
import org.jeecg.modules.system.mapper.TaskCalculatorMapper;
import org.jeecg.modules.system.service.ITaskCalculatorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("TaskCalculatorServiceImpl")
public class TaskCalculatorServiceImpl implements ITaskCalculatorService {

    @Resource
    BizEquipmentMapper bizEquipmentMapper;

    @Resource
    TaskCalculatorMapper taskCalculatorMapper;


    @Override
    public void calMeterHour() {
        QueryWrapper eqCond = new QueryWrapper();
        List<BizEquipmentEntity> equipmentEntityList = bizEquipmentMapper.selectList(eqCond);
        for(BizEquipmentEntity e : equipmentEntityList){

            //清空临时表,临时表的作用就是每次排序
            taskCalculatorMapper.truncateBizMeterDataTmp();

            //插入最近X个月的数据
            taskCalculatorMapper.insertBizMeterDataTmpByMeterData(e.getEqId(),"1");
            taskCalculatorMapper.deleteBizMeterDataUse(e.getEqId(),"1");

            //计算相隔的两个时间点使用的能耗
            taskCalculatorMapper.insertBizMeterDataUse(e.getEqId(),e.getEqType());

            //清理掉原来的表数据
            taskCalculatorMapper.deleteMeterDataHour(e.getEqId());

            //插入数据
            taskCalculatorMapper.insertMeterDataHour(e.getEqId(),e.getEqType());
        }

    }

    @Override
    public void calMeterDay() {
        taskCalculatorMapper.setSqlSafeUpdates();
        taskCalculatorMapper.deleteMeterDataDay("365");
        taskCalculatorMapper.insertMeterDataDay("365");
    }

    @Override
    public void calMeterMonth() {
        taskCalculatorMapper.setSqlSafeUpdates();
        taskCalculatorMapper.deleteMeterDataMonth("12");
        taskCalculatorMapper.insertMeterDataMonth("12");

    }

    @Override
    public void calMeterYear() {
        taskCalculatorMapper.setSqlSafeUpdates();
        taskCalculatorMapper.deleteMeterDataYear("2");
        taskCalculatorMapper.insertMeterDataYear("2");
    }
}
